package com.neuedu.mybaits.dao;

import com.neuedu.common.entity.User;
import com.neuedu.common.util.MyBatisUtils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class UserDao {


    String resource = "mybatis/SqlMapConfig.xml";

    /***
     * 查询 集合（所有）
     * 查询 分页查询
     * 根据抓紧查一条
     * 根据 条件查一条
     *
     *  查询总条数
     *
     * 插入（根据提供的字段插入）
     *
     *
     * 更新（全量更新） 根据主键
     *               根据条件更新
     *
     *
     *   删除    根据主键删除
     *          根据非主键（根据提供的条件）
     *
     *
     */


    /**
     * 根据条件查询集合
     *
     * @return
     */
    public List selectList( ) {

        SqlSession session = null;
        List<User> userList = new ArrayList<>();
        try {
            SqlSessionFactory  sqlSessionFactory = MyBatisUtils.getFactory(resource);
            session = sqlSessionFactory.openSession();
            //执行sql语句
            String statement = "test.selectList";
            userList = session.selectList(statement);

            session.commit();
        } catch (IOException e) {
            session.rollback();
            e.printStackTrace();
        } finally {

            if (session != null) {
                session.close();
            }
        }
        return userList;
    }

    /**
     * 根据主键查询一条
     *
     * @param id
     * @return
     */
    public User selectById(int id) {
        SqlSession session = null;
        User user = null;
        try {

            SqlSessionFactory  sqlSessionFactory = MyBatisUtils.getFactory(resource);
            session = sqlSessionFactory.openSession();
            //默认不自动提交事务

            //执行sql语句
            String statement = "test.selectById";
            user = session.selectOne(statement, id);

            session.commit();
        } catch (IOException e) {
            session.rollback();
            e.printStackTrace();
        } finally {

            if (session != null) {
                session.close();
            }
        }
        return user;
    }

    /**
     * @param user
     * @return
     */
    public int insert(User user) {
        SqlSession session = null;
        int count = 0;
        try {
            SqlSessionFactory  sqlSessionFactory = MyBatisUtils.getFactory(resource);

            //默认不自动提交事务
            session = sqlSessionFactory.openSession();
            //执行sql语句
            String statement = "test.insert";
            count = session.insert(statement, user);

            session.commit();
        } catch (IOException e) {
            session.rollback();
            e.printStackTrace();
        } finally {

            if (session != null) {
                session.close();
            }

        }
        return count;
    }

    /**
     * @param user
     * @return
     */
    public int updateById(User user) {
        SqlSession session = null;
        int update = 0;
        try {
            SqlSessionFactory  sqlSessionFactory = MyBatisUtils.getFactory(resource);
            //默认不自动提交事务
            session = sqlSessionFactory.openSession();

            String statement = "test.updateById";
            update = session.update(statement, user);

            session.commit();
        } catch (IOException e) {
            session.rollback();
            e.printStackTrace();
        } finally {

            if (session != null) {
                session.close();
            }

        }
        return update;
    }

    /**
     * @param id
     * @return
     */
    public int deleteById(int id) {
        SqlSession session = null;
        int delete = 0;
        try {
            SqlSessionFactory  sqlSessionFactory = MyBatisUtils.getFactory(resource);
            //默认不自动提交事务
            session = sqlSessionFactory.openSession();

            String statement = "test.deleteById";
            delete = session.delete(statement, id);

            session.commit();
        } catch (IOException e) {
            session.rollback();
            e.printStackTrace();
        } finally {

            if (session != null) {
                session.close();
            }

        }
        return delete;
    }


}
